package cn.wangzg.course0402;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnOpen(View view){
        //获取editText中的文本
        EditText editText=findViewById(R.id.editText);
        String str=editText.getText().toString();

        Intent intent = new Intent(this,OtherActivity.class);

        intent.putExtra("one",str);
        //传递对象  Serializable实现
        User user=new User();
        user.setId("111");
        user.setName("张三");
        user.setGender("男");
        //intent.putExtra("user",user);
        MyApp.getInstance().setUser(user);
        //传递对象  Parcelable实现
        Person person=new Person("222","李四","男");
        //intent.putExtra("person",person);
        MyApp.getInstance().setPerson(person);
        startActivity(intent);
    }

    public void btnResult(View v ){
        //Intent intent = new Intent(this,OtherActivity.class);
        //startActivityForResult(intent,100);
        Uri uri = Uri.parse("http://m.baidu.com");
        Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == 20) {
                String str = data.getStringExtra("ret");
                EditText editText = findViewById(R.id.editText);
                editText.setText(str);
            }
        }
    }
}
